package cn.com.lsy.product.lepao.util.payUtil.weChatPayUtil;


import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.com.lsy.product.lepao.util.payUtil.weChatPayUtil.weChatPayEntity.WXPayRefundResult;
import com.alipay.api.internal.util.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;


public class ParseXMLUtils {

    /**
     * 1、DOM解析
     */

    @SuppressWarnings("rawtypes")
    public static void beginXMLParse(String xml) {
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml);  // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点smsReport

            System.out.println("锟斤拷锟节碉拷锟角ｏ拷" + rootElt.getName());

            Iterator iters = rootElt.elementIterator("sendResp"); // 获取根节点下的子节点sms

            while (iters.hasNext()) {
                Element recordEle1 = (Element) iters.next();
                Iterator iter = recordEle1.elementIterator("sms");

                while (iter.hasNext()) {
                    Element recordEle = (Element) iter.next();
                    String phone = recordEle.elementTextTrim("phone"); // 拿到sms节点下的子节点stat值

                    String smsID = recordEle.elementTextTrim("smsID"); // 拿到sms节点下的子节点stat值

                    System.out.println(phone + ":" + smsID);
                }
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 2、DOM4j解析XML（支持xpath）
     * 解析的时候自动去掉CDMA
     *
     * @param xml
     */

    public static void xpathParseXml(String xml) {
        try {
            StringReader read = new StringReader(xml);
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(read);
            String xpath = "/xml/appid";
            System.out.print(doc.selectSingleNode(xpath).getText());
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static boolean parseInt(String key) {
        if (!StringUtils.isEmpty(key)) {
            if (key.equals("total_fee") || key.equals("cash_fee") || key.equals("coupon_fee") || key.equals("coupon_count") || key.equals("coupon_fee_0")) {
                return true;
            }
        }

        return false;
    }


    /**
     * @param xml
     * @return Map
     * @description 将xml字符串转换成map
     */
    public static Map<String, String> readStringXmlOut(String xml) {
        Map<String, String> map = new HashMap<String, String>();
        Document doc = null;
        try {
            doc = DocumentHelper.parseText(xml); // 将字符串转为XML
            Element rootElt = doc.getRootElement(); // 获取根节点
            List<Element> list = rootElt.elements();//获取根节点下所有节点
            for (Element element : list) {  //遍历节点
                map.put(element.getName(), element.getText()); //节点的name为map的key，text为map的value
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 解析退款申请
     * 解析的时候自动去掉CDMA
     *
     * @param xml
     */
    @SuppressWarnings("unchecked")
    public static WXPayRefundResult getUnifiedorderResult(String xml) {
        WXPayRefundResult unifieorderResult = new WXPayRefundResult();
        try {
            StringReader read = new StringReader(xml);
            // 创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
            InputSource source = new InputSource(read);
            // 创建一个新的SAXBuilder
            SAXBuilder sb = new SAXBuilder();
            // 通过输入源构造一个Document
            Document doc;
            doc = (Document) sb.build(source);

            Element root = doc.getRootElement();// 指向根节点
            List<org.jdom2.Element> list = ((org.jdom2.Element) root).getChildren();

            if (list != null && list.size() > 0) {
                for (org.jdom2.Element element : list) {
                    System.out.println("key是：" + element.getName() + "，值是：" + element.getText());
                    if ("return_code".equals(element.getName())) {
                        unifieorderResult.setReturn_code(element.getText());
                    }

                    if ("return_msg".equals(element.getName())) {
                        unifieorderResult.setReturn_msg(element.getText());
                    }

                    if ("result_code".equals(element.getName())) {
                        unifieorderResult.setResult_code(element.getText());
                    }
                }
            }

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return unifieorderResult;
    }


}
